<?php

class DefaultController extends Controller
{
	public function actionIndex()
	{
		$model=new Orders('search');
		$model->unsetAttributes();
		if(isset($_GET['Orders']))
			$model->attributes=$_GET['Orders'];

		$this->render('index', array(
			'model'=>$model
		));
	}
    
    public function actionView($id)
    {
        $model = Orders::model()->findByPk($id);
        
         $this->render('view', array(
            'model'=>$model
        ));
    }
    public function actionUpdate($id){
    	$model = Orders::model()->findByPk($id);
    	if($model->status == "P"){
    		$model->updateByPk($model->id, array('status'=>'D')) ;
    	}else{
    		$model->updateByPk($model->id, array('status'=>'P')) ;
    	}
    	$this->redirect(array('view','id'=>$model->id));
    }

        public function actionDelete($id)
    {
        $this->loadModel($id)->delete();
        $criteria = new CDbCriteria;
        $criteria->condition='order_id='.$id;
        OrderBuyers::model()->deleteAll($criteria);
        OrderReceivers::model()->deleteAll($criteria);
        OrderTickets::model()->deleteAll($criteria);
        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if(!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
    }

    public function loadModel($id)
    {
        $model= Orders::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }
}